package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienChuongTrinhHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoanService;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhi;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhiService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienDonXinXetMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienDonXinXetMienGiamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;

import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class XetMienGiamSaveAction{


    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

  

    private TblQLDTTXAHocVienDonXinXetMienGiam tblQLDTTXAHocVienDonXinXetMienGiam; 
    public void setTblQLDTTXAHocVienDonXinXetMienGiam (TblQLDTTXAHocVienDonXinXetMienGiam tblQLDTTXAHocVienDonXinXetMienGiam) {
        this.tblQLDTTXAHocVienDonXinXetMienGiam = tblQLDTTXAHocVienDonXinXetMienGiam;
 }


    private TblQLDTTXAHocVienDonXinXetMienGiamService tblQLDTTXAHocVienDonXinXetMienGiamService; 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 
    
    private TblQLDTTXAHocVienChuongTrinhHocService tblQLDTTXAHocVienChuongTrinhHocService;
    
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    
    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;
    
    private TblQLDTTXAHocVienTaiKhoanService tblQLDTTXAHocVienTaiKhoanService;
    
    private TblQLDTTXAQuiDinhHocPhiService tblQLDTTXAQuiDinhHocPhiService;

    private String tblQLDTTXAHocVienMienGiamStr; 
    public void setTblQLDTTXAHocVienMienGiamStr (String tblQLDTTXAHocVienMienGiamStr) 
 {     this.tblQLDTTXAHocVienMienGiamStr = tblQLDTTXAHocVienMienGiamStr;
 }

    public String getTblQLDTTXAHocVienMienGiamStr() {
       return tblQLDTTXAHocVienMienGiamStr;
 }

    private TblQLDTTXAHocVienMienGiamService tblQLDTTXAHocVienMienGiamService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preUpdate(Message msg, TblQLDTTXAHocVienDonXinXetMienGiam obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXAHocVienDonXinXetMienGiam obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    }
	
	
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_DonXinXetMienGiam", "sua")
    			|| !tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_MienGiam", "sua")){ 
        return "noprivilege"; 
    } 
    
    tblQLDTTXAHocVienDonXinXetMienGiamService = (TblQLDTTXAHocVienDonXinXetMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_DonXinXetMienGiamClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocVienMienGiamService = (TblQLDTTXAHocVienMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_MienGiamClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient");
        
        tblQLDTTXAHocVienChuongTrinhHocService = (TblQLDTTXAHocVienChuongTrinhHocService)  context.getBean("tbl_QLDTTXA_HocVien_ChuongTrinhHocClient");
        
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
        
        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient");

        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
        
        tblQLDTTXAHocVienTaiKhoanService = (TblQLDTTXAHocVienTaiKhoanService) context.getBean("tbl_QLDTTXA_HocVien_TaiKhoanClient");
        
        tblQLDTTXAQuiDinhHocPhiService = (TblQLDTTXAQuiDinhHocPhiService) context.getBean("tbl_QLDTTXA_QuiDinhHocPhiClient");

        msg = new Message(); 
        tblQLDTTXAHocVienDonXinXetMienGiam = tblQLDTTXAHocVienDonXinXetMienGiamService.gettblQLDTTXAHocVienDonXinXetMienGiam(new Long(id)); 

	    List < ConditionReport > cons = new ArrayList<ConditionReport>();
	    {
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("donMGId.id"); 
		    con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getId()); 
		    con.setOperator("eq"); 
		    cons.add(con); 
	    }
	    List<TblQLDTTXAHocVienMienGiam> tblQLDTTXAHocVienMienGiamList = tblQLDTTXAHocVienMienGiamService.findtblQLDTTXAHocVienMienGiamByCondition(cons, 0, 0); 
	    if(tblQLDTTXAHocVienMienGiamList != null && tblQLDTTXAHocVienMienGiamList.size() >0){ 
	         List<String> ids = new ArrayList<String>();  
	         for(TblQLDTTXAHocVienMienGiam obj0 : tblQLDTTXAHocVienMienGiamList ){ 
	             Collections.addAll(ids, obj0.getId().toString());  
	         } 
	         tblQLDTTXAHocVienMienGiamService.deleteListtblQLDTTXAHocVienMienGiam(ids);
	         
	         cons.clear();
	       	   { 
	       	       ConditionReport con = new ConditionReport(); 
	       	       con.setCol("hocVienId.id");
	       	       con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getHocVienId().getId());
	       	       con.setOperator("eq");
	       	       cons.add(con);	
	       	   }
	       	   { 
	       	       ConditionReport con = new ConditionReport(); 
	       	       con.setCol("diem");
	       	       con.setValue1(new Double(12));
	       	       con.setOperator("eq");
	       	       cons.add(con);	
	       	   }
	       	List<TblQLDTTXAHocVienChuongTrinhHoc> tblQLDTTXAHocVienChuongTrinhHocs = tblQLDTTXAHocVienChuongTrinhHocService.findtblQLDTTXAHocVienChuongTrinhHocByCondition(cons, 0, 0);
	       	   
	       	for(TblQLDTTXAHocVienChuongTrinhHoc tblQLDTTXAHocVienChuongTrinhHoc : tblQLDTTXAHocVienChuongTrinhHocs) {
	       		tblQLDTTXAHocVienChuongTrinhHoc.setHoanThanh( false );
	       		   
       		   tblQLDTTXAHocVienChuongTrinhHoc.setDiem(null);

       		   tblQLDTTXAHocVienChuongTrinhHocService.updatetblQLDTTXAHocVienChuongTrinhHoc(tblQLDTTXAHocVienChuongTrinhHoc);	            	   
	        
	       	}
	     }
	    
	    cons.clear();
	    Date today = new Date();
      
      	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

      	ConditionReport con1 = new ConditionReport(); 
          con1.setCol("thoiGianBatDau");
          con1.setValue1(df.format(today));
          con1.setOperator("le");
          con1.setType("date");
          cons.add(con1);	
          
          ConditionReport con2 = new ConditionReport(); 
          con2.setCol("thoiGianKetThuc");
          con2.setValue1(df.format(today));
          con2.setOperator("ge");
          con2.setType("date");
          cons.add(con2);	
          
          TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam = null;
          List<TblQLDTTXAHocVienDangKyMonHoc> tblQLDTTXAHocVienDangKyMonHocs = new ArrayList<TblQLDTTXAHocVienDangKyMonHoc>();
          
          List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocs = new ArrayList<TblQLDTTXAHocVienHocPhiMonHoc>();
          
          TblQLDTTXAQuiDinhHocPhi quiDinhHocPhi = tblQLDTTXAQuiDinhHocPhiService.gettblQLDTTXAQuiDinhHocPhi(new Long(1));
          Double tienHaTang = quiDinhHocPhi.getTienHaTang();
          
      List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
      if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
        tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
    }
      if(tblQLDTTXAHocKyTrongNam != null)
      {
    	  // lay tat ca dang ky mon hoc trong hoc ky cua hoc vien
    	  cons.clear();
      	   { 
      	       ConditionReport con = new ConditionReport(); 
      	       con.setCol("phieuDangKyId.hocVienId.id");
      	       con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getHocVienId().getId());
      	       con.setOperator("eq");
      	       cons.add(con);	
      	   }
      	   { 
    	       ConditionReport con = new ConditionReport(); 
    	       con.setCol("phieuDangKyId.hocKyId.id");
    	       con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
    	       con.setOperator("eq");
    	       cons.add(con);	
    	   }
      	 tblQLDTTXAHocVienDangKyMonHocs = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, 0, 0);
      	 
      	 // lay tat ca hocVienhocPhiMonHoc trong hoc ky cua hoc vien
      	cons.clear();
   	   { 
   	       ConditionReport con = new ConditionReport(); 
   	       con.setCol("hocVienHocPhiId.hocVienId.id");
   	       con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getHocVienId().getId());
   	       con.setOperator("eq");
   	       cons.add(con);	
   	   }
   	   { 
 	       ConditionReport con = new ConditionReport(); 
 	       con.setCol("hocVienHocPhiId.hocKyId.id");
 	       con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
 	       con.setOperator("eq");
 	       cons.add(con);	
 	   }
   	   
   	   tblQLDTTXAHocVienHocPhiMonHocs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
      }
	    
	    JSONArray jsonTblQLDTTXAHocVienMienGiam = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienMienGiamStr); 
	    for (int i = 0; i < jsonTblQLDTTXAHocVienMienGiam.size(); i++) 
	     { 
	         JSONObject jsObj = jsonTblQLDTTXAHocVienMienGiam.getJSONObject(i); 
	         TblQLDTTXAHocVienMienGiam obj0 = new TblQLDTTXAHocVienMienGiam(); 
	       obj0.setDonMGId(tblQLDTTXAHocVienDonXinXetMienGiam); 
	        obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
	       obj0.setDuocMienGiam(jsObj.getBoolean("duocMienGiam")); 
	       obj0.setGhiChu(jsObj.getString("ghiChu"));
	       obj0.setDaXetHP(false);
	       
	       if(obj0.isDuocMienGiam()){
	    	   // tim mon hoc da dang ky truoc khi duoc mien giam
		       boolean daDKMH = false;
		       for(TblQLDTTXAHocVienDangKyMonHoc dkmh : tblQLDTTXAHocVienDangKyMonHocs){
		    	   if(dkmh.getMonHocId().getId().equals(new Long(jsObj.getInt("monHocId")))){
		    		   daDKMH = true;
		    		   break;
		    	   }
		       }
		       
		       if(daDKMH){
		    	   boolean daTinhHocPhi = false;
		    	   
		    	   // tim mon hoc da tinh hoc phi truoc khi duoc mien giam
			       for(TblQLDTTXAHocVienHocPhiMonHoc hpMonHoc : tblQLDTTXAHocVienHocPhiMonHocs){
			    	   if(hpMonHoc.getMonHocId().getId().equals(new Long(jsObj.getInt("monHocId")))){
			    		   daTinhHocPhi = true;
			    		   obj0.setDaXetHP(true);
			    		   // neu da dong hoc phi, tra lai tien vao tai khoan hoc vien
			    		   if(hpMonHoc.isDaDongHP()){
			    			   Double hp = hpMonHoc.getThanhTien() + tienHaTang;
			    			   cons.clear();
			    		   	   { 
			    		   	       ConditionReport con = new ConditionReport(); 
			    		   	       con.setCol("hocVienId.id");
			    		   	       con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getHocVienId().getId());
			    		   	       con.setOperator("eq");
			    		   	       cons.add(con);	
			    		   	   }
			    		   	   List<TblQLDTTXAHocVienTaiKhoan> taikhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
			    		   	   
			    		   	   if(taikhoans != null && taikhoans.size() > 0){
				    		   	   TblQLDTTXAHocVienTaiKhoan taikhoan = taikhoans.get(0);
				    		   	   taikhoan.setSoDuTaiKhoan(taikhoan.getSoDuTaiKhoan() + hp);
				    		   	   tblQLDTTXAHocVienTaiKhoanService.updatetblQLDTTXAHocVienTaiKhoan(taikhoan);
			    		   	   }
			    		   }
			    		   hpMonHoc.setMienMon(true);
		    			   hpMonHoc.setThanhTien(new Double(0));
		    			   hpMonHoc.setDaDongHP(true);
		    			   
		    			   tblQLDTTXAHocVienHocPhiMonHocService.updatetblQLDTTXAHocVienHocPhiMonHoc(hpMonHoc);
			    		   
			    		   break;
			    	   }
			       }
		       }else{
		    	   // neu chua dang ky mon hoc, tu lan dang ky sau se bi tinh hoc phi nhu binh thuong
		    	   obj0.setDaXetHP(true);
		       }
	       }
	       tblQLDTTXAHocVienMienGiamService.createtblQLDTTXAHocVienMienGiam(obj0); 
	       
	       if(obj0.isDuocMienGiam()){
		    // cat nhat diem 12 trong chuong trinh hoc
	     	  cons.clear();
		       	   { 
		       	       ConditionReport con = new ConditionReport(); 
		       	       con.setCol("hocVienId.id");
		       	       con.setValue1(tblQLDTTXAHocVienDonXinXetMienGiam.getHocVienId().getId());
		       	       con.setOperator("eq");
		       	       cons.add(con);	
		       	   }
		       	   { 
		       	       ConditionReport con = new ConditionReport(); 
		       	       con.setCol("monHocId.id");
		       	       con.setValue1(new Long(jsObj.getInt("monHocId")));
		       	       con.setOperator("eq");
		       	       cons.add(con);	
		       	   }
		       	List<TblQLDTTXAHocVienChuongTrinhHoc> tblQLDTTXAHocVienChuongTrinhHocs = tblQLDTTXAHocVienChuongTrinhHocService.findtblQLDTTXAHocVienChuongTrinhHocByCondition(cons, 0, 0);
		       	   
		       	for(TblQLDTTXAHocVienChuongTrinhHoc tblQLDTTXAHocVienChuongTrinhHoc : tblQLDTTXAHocVienChuongTrinhHocs) {
			       		
	    		   tblQLDTTXAHocVienChuongTrinhHoc.setHoanThanh( true );
	    		   
	    		   tblQLDTTXAHocVienChuongTrinhHoc.setDiem(new Double(12));
	
	    		   tblQLDTTXAHocVienChuongTrinhHocService.updatetblQLDTTXAHocVienChuongTrinhHoc(tblQLDTTXAHocVienChuongTrinhHoc);	            	   
		              	       	   
		       	}
	       }
	   } 
	    
	    
        tblQLDTTXAHocVienDonXinXetMienGiam.setTrangThaiId(2);

        preUpdate(msg,tblQLDTTXAHocVienDonXinXetMienGiam); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXAHocVienDonXinXetMienGiamService.updatetblQLDTTXAHocVienDonXinXetMienGiam(tblQLDTTXAHocVienDonXinXetMienGiam); 
           postUpdate(msg, tblQLDTTXAHocVienDonXinXetMienGiam); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_HocVien_DonXinXetMienGiam Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
